<template>
  <!--  编辑/新增 弹窗  -->
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" :footer="null">
    <BasicForm @register="registerForm"/>
    <div style="margin-bottom: 20px">
      <div>附件：</div>
      <div style="line-height: 24px" v-for="(item,index) in files">
        <a :href="item.path" target="_blank"> {{ index + 1 }}.{{ item.name }}</a>
      </div>
      <div v-if="files.length==0">暂无附件</div>
    </div>
    <!--    <Row>-->
    <!--      <slot name="formHeader">任务内容</slot>-->
    <!--    </Row>-->
    <!--    <a-textarea class="myTextarea" v-model:value="contentValue" placeholder="提交的审核内容" show-count/>-->
    <div style="width: 100%;height: 100px"></div>
    <div class="myFooter">
      <a-button block @click="closeModal()">取消</a-button>
      <a-button @click="handleTaskUserPass()" type="primary">通过审核</a-button>
      <a-button style="background: #ff4d4f" @click="handleTaskUserNoPass()" type="primary" danger>
        驳回审核
      </a-button>
    </div>
  </BasicModal>
</template>
<script lang="ts">
import {computed, defineComponent, ref, unref} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {BasicForm, useForm} from '/@/components/Form/index';
import {Tinymce} from '/@/components/Tinymce/index';
import {formSchema} from './schema.data';
import {message, Row} from 'ant-design-vue';

import {TaskUserNoPass, TaskUserPass} from '/@/api/taskCenter/taskCenter';
import {Download, getAccountListAll, getAllRoleList} from '/@/api/system/system';
import {useGlobSetting} from "/@/hooks/setting";
import {useUserStore} from "/@/store/modules/user";

const roleList = await getAllRoleList()
const userList = await getAccountListAll({})
export default defineComponent({
  name: 'DeptModal',
  components: {BasicModal, BasicForm, Tinymce, Row},
  emits: ['success', 'register'],
  setup(_, {emit}) {
    const formData = ref('');
    const files: any = ref([]);
    const isUpdate = ref(true);
    let contentValue = ref('');
    const globSetting = useGlobSetting();

    const userStore = useUserStore();
    const userInfo = userStore.getUserInfo;
    const userInfoFull = userList.find(f => f.user_id == userInfo.userId)
    const roleRemark = roleList.find(f => f.role_id == userInfoFull.role_id).remark
    console.log('roleRemark', roleRemark)


    const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
      labelWidth: 100,
      schemas: formSchema,
      showActionButtonGroup: false,
    });

    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
      console.log('data', data)
      console.log('data.integral', data.integral)
      //设置审核员 默认评分
      if (roleRemark === '一级审核员') {
        if (data.record.integral_first === 0) {
          data.record.integral_first = data.record.integral;
        }
      }
      if (roleRemark === '超级管理员' || roleRemark === '二级审核员') {
        if (data.record.integral_first === 0) {
          data.record.integral_first = data.record.integral;
        }
        if (data.record.integral_second === 0) {
          data.record.integral_second = data.record.integral_first;
        }
      }


      // 设置附件
      files.value = [];
      data.roleRemark = roleRemark;
      formData.value = data.record;


      formData.value['file_list']?.split(',').forEach(f => {
        if (f) {
          files.value.push({
            name: getFileName(f),
            path: globSetting.downloadUrl + f
          })
        }
      })
      console.log('formData', formData.value)
      resetFields();
      setModalProps({confirmLoading: false});
      isUpdate.value = !!data?.isUpdate;

      if (unref(isUpdate)) {
        setFieldsValue({
          ...data.record,
        });
        contentValue.value = data.record.content
      }
    });

    function getFileName(fileName) {
      return fileName.substr(13,fileName.length);
    }

    function findPosition(str, queryStr, pon) {
      var x = str.indexOf(queryStr)
      for (var i = 0; i < pon; i++) {
        x = str.indexOf(queryStr, x + 1)
      }
      return x
    }

    const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

    function handleDownload(item) {
      Download(item).then(res => {
        console.log('download', res)
      })
    }

    async function handleTaskUserPass() {
      const values = await validate();
      await TaskUserPass({...values})
      message.success('审批成功');
      emit('success');
      closeModal();
    }

    async function handleTaskUserNoPass() {
      const values = await validate();
      await TaskUserNoPass({...values, content: contentValue.value})
      message.warning('审批已驳回');
      emit('success');
      closeModal();
    }

    return {
      roleRemark,
      formData,
      files,
      closeModal,
      handleTaskUserPass,
      handleTaskUserNoPass,
      handleDownload,
      getFileName,
      registerModal,
      registerForm,
      getTitle,
      contentValue,
    };
  },
});
</script>
<style>
.myTextarea {
  outline: none;
}

.myTextarea textarea {
  height: 200px;
  outline: none;
}

</style>
<style lang="less" scoped>

.myFooter {
  display: flex;
  flex-direction: row-reverse;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  padding: 0 14px;
  box-sizing: border-box;
  background: #fff;
  align-items: center;
  border-top: 1px solid #ccc;

  button {
    width: 120px;
    margin-left: 10px;
  }
}
</style>
